﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Web;
using System.Web.Mvc;
using DotNetOpenAuth.OAuth2;
using ImaginaryDevelopmentMvc3.App.Auth;

namespace ImaginaryDevelopmentMvc3.App.Auth
{
    public class Facebook : IOauthClient
    {
        public void Authenticate(Uri callback)
        {
            var fb = new FacebookClient();

            fb.RequestUserAuthorization(returnTo: callback);
        }

        public OpenIdentity Verify()
        {
            
            var fb = new FacebookClient();

            IAuthorizationState authorization = fb.ProcessUserAuthorization();
           
            Token = authorization.AccessToken;
            if (Token == null)
                return null;

            AuthUri = "https://graph.facebook.com/me?access_token=" + Uri.EscapeDataString(Token);
            if (AuthUri == null)
               return null;

            var request = WebRequest.Create(AuthUri);
            using (var response = request.GetResponse())
            {
                using (var responseStream = response.GetResponseStream())
                {
                    var graph = FacebookGraph.Deserialize(responseStream);
                    
                    return new OpenIdentity() { Id = graph.Id.ToString(), Username = graph.Name, SiteProvider = "Facebook" };
                }
            }
        }

        #region IOauthClient Members


        public string Token { get; private set; }

        public string AuthUri { get; private set; }

        #endregion
    }
}